<?php
namespace Home\Controller;

use Think\Controller;
use GuzzleHttp\json_encode;

class WindowsClientController extends Controller
{

    public function version()
    {       
        header('Content-type: application/json');
        
        $new = array(
                'url' => 'http://ob2p0ov9p.bkt.clouddn.com/client/qrmaster/QRInn_Installer_v1.0.0.0.exe',
                'version' => '1.0.0.0',
                'minversion' => '1.0.0.0',
                'token' => '',
                'date' => date('Y-m-d'),
                'info' => ''
        );
        $history = array($new);        

        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('MANAGE_PUBLIC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => 'uclbrt',
            'time' => time(),
            'isLatest'=> false,
        );
        $list = $client->getMeizhuVersion($RPCData);
        if (!empty($list) && is_array($list)) {
            $new = $list[0];
            if(count($list)>1)
            {
                unset($list[0]);
                $history = array_values($list);
            }
        }
        
        $data = array(
            'new' => $new,
            'history' => $history,
        );
        echo json_encode($data, JSON_UNESCAPED_UNICODE);
    }    

    public function tokenLogin()
    {
        $param = I('get.');  

        if(!empty($param['lang']))
        {
            $langSet = C('DEFAULT_LANG');
            switch ($param['lang'])
            {
                case "zh-CHS":
                case "zh-CN":
                    $langSet = "zh_cn";
                    break;
                case "zh-CHT":
                case "zh-TW":
                case "zh-HK":
                    $langSet = "zh_tw";
                    break;
                case "en-US":
                    $langSet = "en_us";
                    break;
                
            }
            cookie('think_language', $langSet, 'expire=2592000&httponly=0');            
        }

        if (empty($param['token'])) {
            return;
        }
        $redis = new \Redis();
        $redis->connect(C('REDIS_SERVERIP'), C('REDIS_SERVERPORT'), 30);
        $redis->auth(C('REDIS_AUTH_PASSWORD'));
        $redis->select(11);
        $userId = $redis->Get("socket_token_user:{$param['token']}");
        if ($userId) {
            $adminLoginLogic = A('Common/adminLogin', 'Logic');
            $adminLogin = $adminLoginLogic->getInfo($userId);
    
            // 生成cookie信息
            $guid = guidv4_for_openssl();
            $now = time();
            $RsaEncrypt = new \Org\Util\RSAEncrypt();
            $userSSID = $RsaEncrypt->meizhuEncrypt($adminLogin['id']);
            $userSSPS = cookieUserSSPS($adminLogin['mobile'], $adminLogin['password'], $now);
            cookie(C('COOKIE_SERVER_REMEMBER_ME_UUID'), $guid);
            cookie(C('COOKIE_SERVER_REMEMBER_ME_USSID'), $userSSID);
            cookie(C('COOKIE_SERVER_REMEMBER_ME_USSPS'), $userSSPS);
            cookie(C('COOKIE_SERVER_REMEMBER_ME_PSTM'), $now);    
    
            //清除原有session数据
            session(null);
            // 存储基本信息到session
            session(C('USER_ID'), $adminLogin['id']);
            session(C('USER_NAME'), $adminLogin['name']);
            session(C('USER_MOBILE'), $adminLogin['mobile']);
            session(C('USER_HEAD'), $adminLogin['headpath']);
        }
        echo "<script>window.location.href='/Home/BookPage/index.html';</script>";
    }
}